class Solution {
    public boolean isBalanced(TreeNode root) {
        if(root == null){
            return true;
        }
        int leftHigh = maxDepth(root.left);
        int rightHigh = maxDepth(root.right);
        //左右两个子树的高度差的绝对值不超过 1
        return Math.abs(leftHigh-rightHigh) < 2 && isBalanced(root.left) && isBalanced(root.right);
    }
    int maxDepth(TreeNode root){
        if(root == null){
            return 0;
        }
        int leftHigh = maxDepth(root.left);
        int rightHigh = maxDepth(root.right);
        if(leftHigh >= 0 && rightHigh >= 0 && Math.abs(leftHigh-rightHigh)<=1){
            return Math.max(leftHigh,rightHigh) + 1;
        }else{
            return -1;
        }
    }
}